package Principal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import entidades.Aeropuerto;
import entidades.Vuelo;

public class DummyGenerator {
	Random rnd;

	public DummyGenerator() {
		rnd = new Random();
	}

	protected String generateString(Random rng, String characters, int length) {
		char[] text = new char[length];
		for (int i = 0; i < length; i++) {
			text[i] = characters.charAt(rng.nextInt(characters.length()));
		}
		return new String(text);
	}

	public Aeropuerto getDummyAirport() {
		Aeropuerto airport = new Aeropuerto();
		airport.setName("AIRPORT_" + this.generateString(this.rnd, "abc", 2));
		return airport;
	}

	public Vuelo getDummyFlight(List<Aeropuerto> airports, Date actualTime) throws Exception {
		Vuelo flight = new Vuelo();
		if (airports.size() < 2) {
			throw new Exception("No hay suficientes aeropuertos cargados");
		}

		flight.setCantDisponible(this.rnd.nextInt(400));
		flight.setNroVuelo(String.valueOf(this.rnd.nextInt()));
		flight.setPrecio(this.rnd.nextDouble());

		flight.setSalida(this.getDummyDate(actualTime));
		flight.setLlegada(new Date(flight.getSalida().getTime() + this.rnd.nextInt(54000000)));

		flight.setOrigen(this.getDummyAirport(airports));
		flight.setDestino(this.getDummyAirport(airports, flight.getOrigen()));
		
		return flight;
	}

	protected Aeropuerto getDummyAirport(List<Aeropuerto> airports) {
		return airports.get(this.rnd.nextInt(airports.size()));
	}

	protected Aeropuerto getDummyAirport(List<Aeropuerto> airports, Aeropuerto unavailableAirport) {
		Aeropuerto airport = this.getDummyAirport(airports);
		if (airport.equals(unavailableAirport)) {
			return this.getDummyAirport(airports, unavailableAirport);
		}
		return airport;
	}

	protected Date getDummyDate(Date priorDate) {
		Date date = this.getDummyDate();
		if (date.before(priorDate)) {
			return this.getDummyDate(priorDate);
		}
		return date;
	}

	protected Date getDummyDate() {

		int day = 1 + this.rnd.nextInt(30);
		int month = 1 + this.rnd.nextInt(11);
		int year = 2012 + this.rnd.nextInt(2);

		int hour = this.rnd.nextInt(23);
		int minutes = this.rnd.nextInt(59);

		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy h:m");
		try {
			return format.parse(String.format("%d/%d/%d %d:%d", day, month, year, hour, minutes));
		} catch (Exception e) {
			return this.getDummyDate();
		}
	}

}
